// subject.js
var api = require('../../../../service/api');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    subject: {},
    index: 0,
    isTrue: true,
    page: 1,
    answer: 'A',
    categoryId: 0,
    pageNum: 0,
    checked: true,
    answerA: '',
    answerB: '',
    answerC: '',
    answerD: '',
    collectList:[],
    isCollect:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var Id = options.id;
    var that = this;
    var source = app.data.userInfo.nickName;
    that.setData({
      categoryId: Id
    });
    var url = app.apiUrl.subject.view + '?categoryId=' + that.data.categoryId + '&page=' + that.data.page;
    api.doget(url, function (data) {
      if(data!=null){
        if (data.resultCode == 111) {
          var datas = data.datas;
          if (datas.list.length > 0) {
            that.requestCollects();
            that.setData({
              subject: datas.list[0],
              pageNum: datas.totalPage
            });
          }
        }
      }else{
        wx.navigateTo({ url: '../../error/error' })
      }
    })
  },
  /**
   * 操作收藏
   */
  editCollect:function(e){
    var that = this;
    var id = parseInt(e.currentTarget.dataset.index);
    var source = app.data.userInfo.nickName;
    var url = app.apiUrl.collect.add;
    var collected = !that.data.isCollect;
    if(that.data.isCollect){
      url = app.apiUrl.collect.del;
    }
    var parems={
      url: url,
      data:{
        id: '',
        userName: source,
        subjectId: id
      }
    }
    api.post(parems, function (data) {
      if (data != null) {
        if (data.resultCode == 111) {
          that.requestCollects();
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 3000
          })
          that.setData({
            isCollect: collected
          });
        }
      } else {
        wx.navigateTo({ url: '../../error/error' })
      }
    })
  },
  /**
   * 下一题
   */
  nextSubject: function (e) {
    var that = this;
    var i = that.data.page + 1;
    if (i > that.data.pageNum) {
      wx.showToast({
        title: '已经是最后一题了哦',
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      page: i,
      isTrue: true,
      checked: true,
      answerA: '',
      answerB: '',
      answerC: '',
      answerD: ''
    });
    that.requestSubject();
  },
  /**
   * 上一题
   */
  prevSubject: function (e) {
    var that = this;
    var i = that.data.page - 1;
    if (i < 1) {
      wx.showToast({
        title: '已经是第一题了哦',
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      page: i,
      isTrue: true,
      checked: true,
      answerA: '',
      answerB: '',
      answerC: '',
      answerD: ''
    });
    that.requestSubject();
  },
  /**
   * 请求数据
   */
  requestSubject: function () {
    var that = this;
    var url = app.apiUrl.subject.view + '?categoryId=' + that.data.categoryId + '&page=' + that.data.page;
    api.doget(url, function (data) {
      if (data != null) {
        if (data.resultCode == 111) {
          var datas = data.datas;
          if (datas.list.length > 0) {
            let collected = false;
            for (var i = 0; i < that.data.collectList.length; i++) {
              if (that.data.collectList[i].subjectId == datas.list[0].id) {
                collected = true;
              }
            }
            that.setData({
              subject: datas.list[0],
              pageNum: datas.totalPage,
              isCollect: collected
            });
          }
        }
      } else {
        wx.navigateTo({ url: '../../error/error' })
      }
    })
  },
  requestCollects:function(){
    var that = this;
    var source = app.data.userInfo.nickName;
    var parems={
      url: app.apiUrl.collect.list,
      data:{
        userName: source,
        categoryId:that.data.categoryId
      } 
    };
    api.post(parems, function (data) {
      if (data != null) {
        if (data.resultCode == 111) {
          var datas = data.resultDatas;
          if (datas.length > 0) {
            let collected = false;
            for (var i = 0; i < datas.length; i++) {
              if (datas[i].subjectId == that.data.subject.id) {
                collected = true;
              }
            }
            that.setData({
              collectList: datas,
              isCollect: collected
            });
          }
        }
      } else {
        wx.navigateTo({ url: '../../error/error' })
      }
    })
  },
  radioChange: function (e) {
    var that = this;
    if (this.data.checked) {
      var i = that.data.index;
      if (that.data.isMulti) {
        answerM += e.detail.value;
        if (answerM.length = that.data.chooseSubjects[i].answer.length) {
          if (answerM != that.data.chooseSubjects[i].answer) {
            that.setData({
              isTrue: false,
              checked: false
            });
          } else {
            that.setData({
              isTrue: true,
              checked: false
            });
          }
        }
      } else {
        if (e.detail.value != that.data.chooseSubjects[i].answer) {
          that.setData({
            isTrue: false,
            checked: false
          });
        } else {
          that.setData({
            isTrue: true,
            checked: false
          });
        }
      }
    }
  },
  /**
   * 检查答案
   */
  answerCheck: function (e) {
    var that = this;
    if (this.data.checked) {
      var index = parseInt(e.currentTarget.dataset.index);
      var answer = '';
      switch (index) {
        case 1: {
          answer = 'A';
        } break;
        case 2: {
          answer = 'B';
        } break;
        case 3: {
          answer = 'C';
        } break;
        case 4: {
          answer = 'D';
        } break;
      }
      that.setData({
        answer: answer
      })
      if (answer == that.data.subject.answer) {
        switch (index) {
          case 1: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: 'answer_true',
              answerB: '',
              answerC: '',
              answerD: ''
            });
          } break;
          case 2: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: '',
              answerB: 'answer_true',
              answerC: '',
              answerD: ''
            });
          } break;
          case 3: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: 'answer_true',
              answerD: ''
            });
          } break;
          case 4: {
            that.setData({
              isTrue: true,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: '',
              answerD: 'answer_true'
            });
          } break;
        }
      } else {
        switch (index) {
          case 1: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: 'answer_false',
              answerB: '',
              answerC: '',
              answerD: ''
            });
          } break;
          case 2: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: '',
              answerB: 'answer_false',
              answerC: '',
              answerD: ''
            });
          } break;
          case 3: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: 'answer_false',
              answerD: ''
            });
          } break;
          case 4: {
            that.setData({
              isTrue: false,
              checked: false,
              answerA: '',
              answerB: '',
              answerC: '',
              answerD: 'answer_false'
            });
          } break;
        }
      }
    }
  }
})